package com.mxx.common.netty;


import com.mxx.common.netty.http.NettyHttpHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.logging.LoggingHandler;

/**
 * @Auther: Mr. Zeng
 * @Date: 2020/6/29 17:00
 * @Description: 暂无描述
 */
public class NettyHttpChannelInitializer extends ChannelInitializer<SocketChannel> {


    private NettyHttpHandler nettyHttpHandler;
    private NettyServiceProperties nettyServiceProperties;

    public NettyHttpChannelInitializer(NettyServiceProperties nettyServiceProperties, NettyHttpHandler nettyHttpHandler) {
        this.nettyHttpHandler = nettyHttpHandler;
        this.nettyServiceProperties = nettyServiceProperties;
    }

    @Override
    protected void initChannel(SocketChannel ch) {
        ch.pipeline().addLast("logging", new LoggingHandler());//设置log监听器，并且日志级别为debug，方便观察运行流程
        ch.pipeline().addLast("http-codec", new HttpServerCodec());//设置解码器
        ch.pipeline().addLast("aggregator", new HttpObjectAggregator(nettyServiceProperties.getMaxContextLength()));//聚合器，使用websocket会用到
        ch.pipeline().addLast("bizHandler", nettyHttpHandler);//自定义的业务handler
    }
}
